<template>
  <Button
    :type="type"
    :class="disabled ? 'dissendSmsBtn':''"
    class="sendSmsBtn"
    @click="run"
    :disabled="disabled || time > 0">
    {{ text }}
  </button>
</template>

<script>
  export default {
    name: 'time-count-btn',
    props: {
      type: {
        type: String
      },
      second: {
        type: Number,
        default: 60
      }
    },
    data () {
      return {
        time: 0,
        disabled: false
      }
    },
    methods: {
      run () {
        this.$emit('run')
      },
      start () {
        this.time = this.second
        this.timer()
      },
      stop () {
        this.time = 0
      },
      setDisabled (val) {
        this.disabled = val
      },
      timer () {
        if (this.time > 0) {
          this.time --
          setTimeout(this.timer, 1000)
        } else {
          this.disabled = false
        }
      }
    },
    computed: {
      text () {
        return this.time > 0 ? this.time + 's 后可再获取' : '获取短信验证码'
      }
    }
  }
</script>

<style lang="less">
.send-sms-btn {
  position: absolute;
  right: 0px;
  height: 34px;
  line-height: 34px;
  border-radius: 3px;
  background: #ffb400;
  border: none;
  padding: 0 6px;
  color: #fff;
  display: inline-block;
  width: 90px;
  top: 12px;
}
.sendSmsBtn.dissendSmsBtn{
  background-color: #FFE39F;
}
</style>
